Imports System.Xml


''' <summary>
''' This Class holds the car configuration
''' </summary>
''' <remarks>BikiRom Variables, Global Variables, Tables
''' </remarks>
<Serializable(), ObfuscationAttribute(Exclude:=True, ApplyToMembers:=True)> Friend Class CarConfig

    Private m_globalVars As Vars = Nothing
    Private m_bikiVars As BVars = Nothing
    Private m_tables As TableHolder = Nothing

    Private m_series As String = ""
    Private m_engine As String = ""
    Private m_configID As Integer = 0


    Public Property Series() As String
        Get
            Return m_series
        End Get
        Set(ByVal value As String)
            m_series = value
        End Set
    End Property
    Public Property Engine() As String
        Get
            Return m_engine
        End Get
        Set(ByVal value As String)
            m_engine = value
        End Set
    End Property
    Public Property ConfigID() As Integer
        Get
            Return m_configID
        End Get
        Set(ByVal value As Integer)
            m_configID = value
        End Set
    End Property

    Friend Function load(ByVal xmlFile As String) As Boolean
        Dim ret As Boolean = False
        Try
            Dim s_xml As XmlTextReader = New XmlTextReader(xmlFile)
            If Not IsNothing(s_xml) Then
                While s_xml.Read
                    If s_xml.NodeType = XmlNodeType.Element Then
                        Select Case LCase(s_xml.Name)
                            Case "series"
                                If s_xml.Read Then m_series = s_xml.ReadContentAsString
                            Case "engine"
                                If s_xml.Read Then m_engine = s_xml.ReadContentAsString
                            Case "configid"
                                If s_xml.Read Then m_configID = s_xml.ReadContentAsInt
                            Case "global"
                                m_globalVars = Me.loadVars(s_xml.ReadSubtree)
                            Case "bikirom"
                                m_bikiVars = Me.loadBVars(s_xml.ReadSubtree)
                            Case "tableholder"
                                m_tables = Me.loadTableHolder(s_xml.ReadSubtree)
                        End Select
                    End If
                End While
            End If

            ret = True
        Catch
            ret = False
        End Try
        Return ret
    End Function
    Private Function loadDescription(ByRef s_xml As XmlReader) As BikDescription
        Dim f_return As BikDescription = Nothing
        Dim f_desc As String = ""
        Dim f_param As String = ""
        Dim f_tooltip As String = ""
        Dim f_helpID As Integer = 0
        If Not IsNothing(s_xml) Then
            While s_xml.Read
                If s_xml.NodeType = XmlNodeType.Element Then
                    Select Case LCase(s_xml.Name)
                        Case "description"
                            If s_xml.Read And Not s_xml.Value = "" Then f_desc = s_xml.ReadContentAsString
                        Case "parameters"
                            If s_xml.Read And Not s_xml.Value = "" Then f_param = s_xml.ReadContentAsString
                        Case "tooltip"
                            If s_xml.Read And Not s_xml.Value = "" Then f_tooltip = s_xml.ReadContentAsString
                        Case "helpid"
                            If s_xml.Read And Not s_xml.Value = "" Then f_helpID = s_xml.ReadContentAsInt
                    End Select
                End If
            End While
            f_return = New BikDescription(f_desc)
            f_return.HelpID = f_helpID
            f_return.Parameters = f_param
            f_return.ToolTip = f_tooltip
        End If
        Return f_return
    End Function
    Private Function loadScale(ByRef s_xml As XmlReader) As Scale
        Dim f_scale As Scale = Nothing
        Dim f_address As Integer = 0
        Dim f_overlay As String() = Nothing
        Dim f_desc As BikDescription = Nothing
        Dim f_mult As Double = 1
        Dim f_pre As Double = 0
        Dim f_div As Double = 0
        Dim f_offset As Double = 0
        Dim f_scheme As Int16 = -1
        If Not IsNothing(s_xml) Then
            While s_xml.Read
                If s_xml.NodeType = XmlNodeType.Element Then
                    Select Case LCase(s_xml.Name)
                        Case "address"
                            If s_xml.Read And Not s_xml.Value = "" Then f_address = s_xml.ReadContentAsInt
                        Case "multiplier"
                            If s_xml.Read And Not s_xml.Value = "" Then f_mult = s_xml.ReadContentAsDouble
                        Case "overlay"
                            If s_xml.Read And Not s_xml.Value = "" Then f_overlay = cv_arrayFromString(s_xml.ReadContentAsString)
                        Case "preDivider"
                            If s_xml.Read And Not s_xml.Value = "" Then f_pre = s_xml.ReadContentAsDouble
                        Case "offset"
                            If s_xml.Read And Not s_xml.Value = "" Then f_offset = s_xml.ReadContentAsDouble
                        Case "bikdescription"
                            f_desc = Me.loadDescription(s_xml.ReadSubtree)
                        Case "scheme"
                            If s_xml.Read And Not s_xml.Value = "" Then f_scheme = s_xml.ReadContentAsInt
                    End Select
                End If
            End While
            f_scale = New Scale(f_address, f_mult, f_offset, f_pre, f_desc)
            f_scale.Trace = f_scheme
            f_scale.Overlay = f_overlay
        End If
        Return f_scale
    End Function
    Private Function loadTable(ByRef s_xml As XmlReader) As Table
        Dim f_return As Table = Nothing
        Dim f_name As String = ""
        Dim f_desc As BikDescription = Nothing
        Dim f_addr As Integer = 0
        Dim f_width As Short = 1
        Dim f_height As Short = 1
        Dim f_bytesize As Byte = 1
        Dim f_multiplier As Double = 1
        Dim f_predivider As Double = 0
        Dim f_offset As Double = 0
        Dim f_marginal As Integer = 0
        Dim f_axisswap As Boolean = False
        Dim f_wInvert As Boolean = False
        Dim f_hInvert As Boolean = False
        Dim f_dispMode As DisplayMode = DisplayMode.BK_DISP_RAW
        Dim f_dispScheme As DisplayScheme = DisplayScheme.BK_SCH_EARLY
        Dim f_wScale As New Scale()
        Dim f_hScale As New Scale

        If Not IsNothing(s_xml) Then
            While s_xml.Read
                If s_xml.NodeType = XmlNodeType.Element Then
                    Select Case LCase(s_xml.Name)
                        Case "name"
                            If s_xml.Read Then f_name = s_xml.ReadContentAsString
                        Case "bikdescription"
                            f_desc = Me.loadDescription(s_xml.ReadSubtree)
                        Case "address"
                            If s_xml.Read Then f_addr = s_xml.ReadContentAsInt
                        Case "width"
                            If s_xml.Read Then f_width = s_xml.ReadContentAsInt
                        Case "height"
                            If s_xml.Read Then f_height = s_xml.ReadContentAsInt
                        Case "bytesize"
                            If s_xml.Read And Not s_xml.Value = "" Then f_bytesize = s_xml.ReadContentAsInt
                        Case "multiplier"
                            If s_xml.Read And Not s_xml.Value = "" Then f_multiplier = s_xml.ReadContentAsDouble
                        Case "predivider"
                            If s_xml.Read And Not s_xml.Value = "" Then f_predivider = s_xml.ReadContentAsDouble
                        Case "offset"
                            If s_xml.Read And Not s_xml.Value = "" Then f_offset = s_xml.ReadContentAsDouble
                        Case "marginal"
                            If s_xml.Read And Not s_xml.Value = "" Then f_marginal = s_xml.ReadContentAsInt
                        Case "axisswap"
                            If s_xml.Read And Not s_xml.Value = "" Then f_axisswap = s_xml.ReadContentAsBoolean
                        Case "widthinvert"
                            If s_xml.Read And Not s_xml.Value = "" Then f_wInvert = s_xml.ReadContentAsBoolean
                        Case "heightinvert"
                            If s_xml.Read And Not s_xml.Value = "" Then f_hInvert = s_xml.ReadContentAsBoolean
                        Case "dispmode"
                            If s_xml.Read And Not s_xml.Value = "" Then f_dispMode = s_xml.ReadContentAsInt
                        Case "disscheme"
                            If s_xml.Read And Not s_xml.Value = "" Then f_dispScheme = s_xml.ReadContentAsInt
                        Case "widthscale"
                            f_wScale = Me.loadScale(s_xml.ReadSubtree)
                        Case "heightscale"
                            f_hScale = Me.loadScale(s_xml.ReadSubtree)
                    End Select
                End If
            End While
            f_return = New Table(f_addr, f_width, f_height, f_name, f_bytesize)
            f_return.setAxis(f_axisswap, f_wInvert, f_hInvert, f_dispMode, f_dispScheme)
            f_return.setDisplay(f_multiplier, f_predivider, f_offset, f_marginal)
            f_return.Description = f_desc
            If Not IsNothing(f_hScale) Then f_return.setHeightAxis(f_hScale)
            If Not IsNothing(f_wScale) Then f_return.setWidthAxis(f_wScale)
        End If
        Return f_return
    End Function
    Private Function loadTableCont(ByRef s_xml As XmlReader) As TableCont
        Dim f_return As TableCont = Nothing
        Dim f_name As String = ""
        Dim f_desc As BikDescription = Nothing
        Dim f_tables As New ArrayList()
        If Not IsNothing(s_xml) Then
            While s_xml.Read
                If s_xml.NodeType = XmlNodeType.Element Then
                    Select Case LCase(s_xml.Name)
                        Case "name"
                            If s_xml.Read Then f_name = s_xml.ReadContentAsString
                        Case "bikdescription"
                            f_desc = Me.loadDescription(s_xml.ReadSubtree)
                        Case "table"
                            f_tables.Add(Me.loadTable(s_xml.ReadSubtree))
                    End Select
                End If
            End While
            f_return = New TableCont(f_name)
            f_return.description = f_desc
            If f_tables.Count >= 1 Then
                For Each tab As Table In f_tables.ToArray
                    f_return.addTable(tab)
                Next
            End If
        End If
        Return f_return
    End Function
    Private Function loadTableHolder(ByRef s_xml As XmlReader) As TableHolder
        Dim f_return As TableHolder = Nothing
        Dim f_name As String = ""
        Dim f_desc As BikDescription = Nothing
        Dim f_tables As New ArrayList()
        If Not IsNothing(s_xml) Then
            While s_xml.Read
                If s_xml.NodeType = XmlNodeType.Element Then
                    Select Case LCase(s_xml.Name)
                        Case "name"
                            If s_xml.Read Then f_name = s_xml.ReadContentAsString
                        Case "bikdescription"
                            f_desc = Me.loadDescription(s_xml.ReadSubtree)
                        Case "tablecont"
                            f_tables.Add(Me.loadTableCont(s_xml.ReadSubtree))
                    End Select
                End If
            End While
            f_return = New TableHolder(f_name)
            f_return.Description = f_desc
            If f_tables.Count >= 1 Then
                For Each tab As TableCont In f_tables.ToArray
                    f_return.addNode(tab)
                Next
            End If
        End If
        Return f_return
    End Function
    Private Function loadVar(ByRef s_xml As XmlReader) As Var
        Dim f_return As Var = Nothing
        Dim f_name As String = ""
        Dim f_address As Integer = 0
        Dim f_mult As Double = 1
        Dim f_bitsize As Byte = 8
        Dim f_bit As Byte = 0
        Dim f_offset As Double = 0

        Dim f_desc As BikDescription = Nothing
        If Not IsNothing(s_xml) Then
            While s_xml.Read
                If s_xml.NodeType = XmlNodeType.Element Then
                    Select Case LCase(s_xml.Name)
                        Case "name"
                            If s_xml.Read Then f_name = s_xml.ReadContentAsString
                        Case "bikdescription"
                            f_desc = Me.loadDescription(s_xml.ReadSubtree)
                        Case "address"
                            If s_xml.Read Then f_address = s_xml.ReadContentAsInt
                        Case "multiplier"
                            If s_xml.Read And Not s_xml.Value = "" Then f_mult = s_xml.ReadContentAsDouble
                        Case "bitsize"
                            If s_xml.Read And Not s_xml.Value = "" Then f_bitsize = s_xml.ReadContentAsInt
                        Case "bit"
                            If s_xml.Read And Not s_xml.Value = "" Then f_bit = s_xml.ReadContentAsInt
                        Case "offset"
                            If s_xml.Read And Not s_xml.Value = "" Then f_mult = s_xml.ReadContentAsDouble
                    End Select
                End If
            End While
            f_return = New Var(f_name, f_desc, f_address, f_mult, f_bitsize, f_bit)
            f_return.Offset = f_offset
        End If
        Return f_return
    End Function
    Private Function loadVars(ByRef s_xml As XmlReader) As Vars
        Dim f_return As Vars = Nothing
        Dim f_name As String = ""
        Dim f_vars As New ArrayList
        Dim f_desc As String = ""
        If Not IsNothing(s_xml) Then
            While s_xml.Read
                If s_xml.NodeType = XmlNodeType.Element Then
                    Select Case LCase(s_xml.Name)
                        Case "description"
                            If s_xml.Read And Not s_xml.Value = "" Then f_desc = s_xml.ReadContentAsString
                        Case "variable"
                            f_vars.Add(Me.loadVar(s_xml.ReadSubtree))
                    End Select
                End If
            End While
            f_return = New Vars(f_desc)
            If f_vars.Count >= 1 Then
                For Each tab As Var In f_vars.ToArray
                    f_return.addNode(tab)
                Next
            End If
        End If
        Return f_return
    End Function
    Private Function loadBvar(ByRef s_xml As XmlReader) As BVar
        Dim f_return As BVar = Nothing
        Dim f_name As String = ""
        Dim f_value As Double = 0
        Dim f_desc As BikDescription = Nothing
        If Not IsNothing(s_xml) Then
            While s_xml.Read
                If s_xml.NodeType = XmlNodeType.Element Then
                    Select Case LCase(s_xml.Name)
                        Case "name"
                            If s_xml.Read Then f_name = s_xml.ReadContentAsString
                        Case "bikdescription"
                            f_desc = Me.loadDescription(s_xml.ReadSubtree)
                        Case "value"
                            If s_xml.Read Then f_value = s_xml.ReadContentAsDouble
                    End Select
                End If
            End While
            f_return = New BVar(f_name, f_desc)
            f_return.Value = f_value
        End If
        Return f_return
    End Function
    Private Function loadBVars(ByRef s_xml As XmlReader) As BVars
        Dim f_return As BVars = Nothing
        Dim f_name As String = ""
        Dim f_vars As New ArrayList
        Dim f_desc As String = ""
        If Not IsNothing(s_xml) Then
            While s_xml.Read
                If s_xml.NodeType = XmlNodeType.Element Then
                    Select Case LCase(s_xml.Name)
                        Case "description"
                            If s_xml.Read And Not s_xml.Value = "" Then f_desc = s_xml.ReadContentAsString
                        Case "variable"
                            f_vars.Add(Me.loadBvar(s_xml.ReadSubtree))
                    End Select
                End If
            End While
            f_return = New BVars(f_desc)
            If f_vars.Count >= 1 Then
                For Each tab As BVar In f_vars.ToArray
                    f_return.addNode(tab)
                Next
            End If
        End If
        Return f_return
    End Function

    Friend Function getGlobals() As Vars
        Return m_globalVars
    End Function
    Friend Function getBikiVars() As BVars
        Return m_bikiVars
    End Function
    Friend Function getTables() As TableHolder
        Return m_tables
    End Function
    Friend Sub setTables(ByRef Tables As TableHolder)
        m_tables = Tables
    End Sub
    Friend Sub setGlobals(ByRef Globals As Vars)
        m_globalVars = Globals
    End Sub
    Friend Sub setBikiVars(ByRef BVars As BVars)
        m_bikiVars = BVars
    End Sub
End Class
